<?php
/**
 * Created by PhpStorm.
 * User: yangh
 * Date: 2017/7/30
 * Time: 16:45
 */

namespace app\api\validate;


use app\lib\exception\BaseException;
use app\lib\exception\ParameterException;
use think\Exception;
use think\Request;
use think\Validate;

class BaseValidate extends Validate
{
    public function goCheck(){
        //获取http传入参数
        //对参数进行校验
        $request = Request::instance();
        $params = $request->param();
        $result = $this->batch()->check($params);
        if(!$result){

            $e = new BaseException([
                'msg' => $this->error
            ]);
            throw $e;
        }else{
            return true;
        }
    }

    protected function isPositiveIntrger($value,$rule = '',$data = '',$field =''){

        if(is_numeric($value) && is_int($value + 0) && ($value + 0)>0){
            return true;
        }else{
            return false;
        }
    }

    protected function isNotEmpty($value,$rule='',$data='',$field=''){
        if (empty($value)){
            return false;
        }else{
            return true;
        }
    }

    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public function getDataByRule($arrays)
    {
        if (array_key_exists('user_id', $arrays) |
            array_key_exists('uid', $arrays) |
            array_key_exists('id', $arrays)
        )
        {
            // 不允许包含user_id或者uid，防止恶意覆盖user_id外键
            throw new ParameterException(
                [
                    'msg' => '参数中包含有非法的参数名user_id、uid、id'
                ]);
        }

        $newArray = [];

        foreach ($this->rule as $key => $value)
        {
            if(array_key_exists($key,$arrays)){
                $newArray[$key] = $arrays[$key];
            }
        }
        return $newArray;
    }

    protected function isLabelArray($value){
        foreach ($value as $key){
            $result = preg_match('/^[^_|\\d][\\x4E00-\\x9FA5\\xF900-\\xFA2D|_|A-Z-a-z|\\d]{5,10}$/', $key);
            if(!$result){
                return false;
            }
        }
        return true;
    }
}